<?php
namespace Hive;
/**
 * Workflow Object. 
 * 
 * It represant the workflow header information (label, type, description...)
 *
 * @author Xavier VILAIN
 * @package Hive
 * @property int 	$wid			Company Id
 * @property string $type			Type of workflow
 * @property string $category		Workflow Category, meaning target ojbect (Mission, Contact...)
 * @property string $label			Label will be shown in customizing screens
 * @property string $description	Workflow description Company Website
 * @property bool 	$loekz 			Deletion Flag if true is an been deleted
 */
use Hive\Workflow\Step;

class Workflow extends \Fox\Object\Versioned {
	
	
	/**
	 * Link to Workflow Steps
	 * @var string
	 */
	const LINK_STEPS = 'steps';
	
	/**
	 * 
	 * Render Workflow 
	 * 
	 * @return 
	 */
	public function render($tree,$items) {
		$removedList = $html = null;
		foreach($tree->getChildren() as $child) {
			$stepid = $child->getProperty('id');
			if(empty($stepid)) {
				$stepid = 0;
			}
			$htmlStep = '<div class="mission-step">
							<span class="ui-widget-header">'.$child->getProperty('label').'</span>
								<ul id="workflow_'.$stepid.'" class="ui-sortable" data-status="'.$stepid.'">';
			
			if(isset($items[$stepid])) {
				foreach($items[$stepid] as $item ) {
					$htmlStep .= '<li class="ui-state-default ui-draggable {\'contactid\':'.$item['contactid'].',\'missionid\':'.$item['missionid'].'}">'.$item['html'].'</li>';
				}
			} 	
			$htmlStep .= '</ul></div>';
			$html .= $htmlStep;
			$html .= $this->render($child,$items);
		}
		return $html;
	}
	
	/**
	 * 
	 * Build the workflow tree.
	 * 
	 * This method will return the tree as a Fox\Node object and the array of 
	 * each nodes to facilitate items adding
	 * 
	 * @return array ('tree' => Tree,'nodes' => all nodes)
	 */
	public function tree() {
		$nodes = array();
		$root = new \Fox\Node();
		foreach($this->steps() as $step) {
			$type = $step->type;
			if($type == Step::TYPE_STANDARD) {
				$parentid = $step->parentid;
				$node = new \Fox\Node(array('id' => $step->stepid,
											'label' => $step->label,
											'parentid' => $step->parentid
									));
				if(isset($nodes[$step->parentid])) {
					$nodes[$step->stepid] = $nodes[$step->parentid]->add($node);
				} else {
					$nodes[$step->stepid] = $root->add($node);
				}
			}
		}
		return array($root,$nodes);
	}
	
	/**
	 * Return Workflow exclusion step
	 * @return Workflow\Step
	 */
	public function exclusionStep() {
		foreach($this->steps() as $step) {
			if($step->type == Step::TYPE_CLOSING) {
				return $step;
			}
		}
	}
	
	/**
	 * (non-PHPdoc)
	 * @see Fox.Object::getData()
	 */
	public function getData() 
	{
		$data = parent::getData();
		$data['steps'] = $this->steps();
		return $data;
	}
	
	/**
	 * 
	 * @return string Workflow label
	 */
	public function label() {
		return $this->label;
	}
 }
\Fox\Dao::infos(Workflow::className(),new \Fox\Object\Info('workflows', \Fox\Databases::DB_APP));

// Link To Worflow Steps
\Fox\Object\Links::add(\Fox\Dao::link(Workflow\Step::className(),Workflow\Step::LINK_WORKFLOW)
	->to(Workflow::className(), Workflow::LINK_STEPS)
	->on('wid')
	->orderBy('parentid')
	->versionFrom()
);